/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.ban.ycl.wx.audio.server.common;

import cn.ac.iie.di.ban.ycl.wx.audio.server.config.Configuration;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicInteger;
import org.apache.log4j.Logger;

/**
 *
 * @author AlexMu
 */
public class RuntimeEnv {

    private static Configuration conf = null;
    public static final String JETTY_SERVER = "jettyServer";
    public static final String JETTY_PORT = "jettyPort";
    public static final String JETTY_THREADNUM = "jettyThreadNum";
    public static final String LIMITINT = "limitint";//最大请求数，可以用于控制并发不做请求的缓存
    public static final String requestDir = "requestDir";//请求sjson写该文件夹，通过网闸进行互联网区下载
    public static final String responseDir = "responseDir";//响应文件放该文件夹
    public static final String loongstore = "loongstore";//音频文件龙存路径
    public static final ConcurrentHashMap<String, String> responseMap = new ConcurrentHashMap(2048);
    public static final LinkedBlockingQueue<String> requestQueue = new LinkedBlockingQueue(1024);
    //本地文件生成路径
    public final static String catchFilePath = "catchFilePath";
    public final static String fileSize = "fileSize";
    public final static String timeOut = "timeOut";
    public final static String writeJsonThreadSize = "writeJsonThreadSize";
    public final static String suffixestr = "suffixestr";
    public static final String WXAUDIOLIMIT = "WXAUDIOLIMIT";
    public static AtomicInteger wxAudioUrlQueryLimit = new AtomicInteger(0);

    private static Map<String, Object> dynamicParams = new HashMap<String, Object>();
    static Logger logger = Logger.getLogger(RuntimeEnv.class.getName());

    public static boolean initialize(Configuration pConf) throws Exception {

        if (pConf == null) {
            logger.error("configuration object is null");
            throw new Exception("configuration object is null");
        }

        conf = pConf;

        setKeyAndValue(JETTY_PORT, "jettyPort");
        setKeyAndValue(JETTY_SERVER, "jettyServer");
        setKeyAndValue(JETTY_THREADNUM, "jettyThreadNum");
        setKeyAndValue(LIMITINT, "limitint");
        setKeyAndValue(requestDir, "requestDir");
        setKeyAndValue(responseDir, "responseDir");
        setKeyAndValue(loongstore, "loongstore");
        setKeyAndValue(catchFilePath, "catchFilePath");
        setKeyAndValue(fileSize, "commit");
        setKeyAndValue(timeOut, "timeOut");
        setKeyAndValue(writeJsonThreadSize, "writeJsonThreadSize");
        setKeyAndValue(suffixestr, "suffixestr");
        setKeyAndValue(WXAUDIOLIMIT, "WXAUDIOLIMIT");

        return true;
    }

    public static boolean setKeyAndValue(String KEY, String parameter) throws Exception {
        String value = conf.getString(KEY, "");
        if (value.isEmpty()) {
            logger.error("parameter " + parameter + " does not exist or is not defined");
            throw new Exception("parameter " + parameter + " does not exist or is not defined");
        }
        addParam(KEY, value);
        return true;
    }

    public static void dumpEnvironment() {
        conf.dumpConfiguration();
    }

    public static void addParam(String pParamName, Object pValue) {
        synchronized (dynamicParams) {
            dynamicParams.put(pParamName, pValue);
        }
    }

    public static Object getParam(String pParamName) {
        return dynamicParams.get(pParamName);
    }
}
